package com.centling.smartSealForPhone.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.bean.ManagedUser;
import com.centling.smartSealForPhone.constants.SPConstants;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.constants.UserType;
import com.centling.smartSealForPhone.utils.GlideApp;
import com.centling.smartSealForPhone.utils.LogUtil;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.SPUtil;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.centling.smartSealForPhone.view.RoundImgView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fionera
 */
public class UserDetailActivity extends TitleBarActivity implements View.OnClickListener {
    private RoundImgView avatar;
    private TextView username;
    private TextView realName;
    private TextView phone;
    private TextView delete;
    private TextView resetPwd;
    private TextView department;

    private ManagedUser user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail);
        setTitleBarText("用户信息");
        initView();
        setData();
    }

    public void initView() {
        avatar = (RoundImgView) findViewById(R.id.user_info_avatar);
        username = (TextView) findViewById(R.id.user_info_username);
        realName = (TextView) findViewById(R.id.user_info_realname);
        phone = (TextView) findViewById(R.id.user_info_phone);
        delete = (TextView) findViewById(R.id.user_info_delete);
        resetPwd = (TextView) findViewById(R.id.user_info_reset);
        department = (TextView) findViewById(R.id.user_info_department);

        resetPwd.setOnClickListener(this);
        delete.setOnClickListener(this);
    }

    public void setData() {
        String json = getIntent().getStringExtra("user_info");
        user = new Gson().fromJson(json, ManagedUser.class);
        LogUtil.d(user.toString());

        GlideApp.with(UserDetailActivity.this).load(user.getAvatar()).placeholder(R.drawable.iv_avatar_default)
                .into(avatar);
        if (user.getuserId().equals(SPUtil.getString(SPConstants.USER_ID)) || TextUtils.equals(
                user.getUserType(), UserType.EQUIP_COMMON_USER)) {
            delete.setVisibility(View.GONE);
        }
        username.setText(user.getUserName());
        realName.setText(user.getRealName());
        phone.setText(user.getPhone());
        department.setText(user.getDepartment());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_info_delete:
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(UserDetailActivity.this);
                dialogBuilder.setTitle("提示");
                dialogBuilder.setMessage("确定删除该用户吗");
                dialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog, int i) {

                        Map<String, String> param = new HashMap<>(5);
                        deleteJsonEnqueue(UrlConstants.DELETE_USER + user.getuserId(), param, new OkHttpUtil.NetCallBack() {
                            @Override
                            public void onSucceed(String json) {
                                dialog.dismiss();
                                ShowToast.shortTime("删除用户成功");
                                finish();
                            }

                            @Override
                            public void onFailed(String reason) {
                                dialog.dismiss();
                                ShowToast.shortTime(reason);
                            }
                        });
                    }
                });

                dialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.dismiss();
                    }
                });

                dialogBuilder.show();
                break;
            case R.id.user_info_reset:
                Intent intent = new Intent(UserDetailActivity.this,
                        ResetUserPasswordActivity.class);
                intent.putExtra("user_id", user.getuserId());
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
